/* modularity_mat_main.c */

#include <stdio.h>
#include "../common/modularity_mat.h"

#ifdef MTRACE
#include <mcheck.h>
#endif

int main(int argc, char** argv)
{
	int result = 0;
	int return_value = 1, elements_no = 0;
	sparse_matrix_lst* A = NULL;
	int* g = NULL;
	elem **B = NULL, **gen_B_g = NULL;
	elem *f = NULL;


	#ifdef MTRACE
	mtrace(); /* Starts the recording of memory allocations and releases */
	#endif
	
	
	if (argc != 3)
	{
		result = printf("\n usage: modularity_mat <adjacency-mat-file> "
						" <group-file> \n");
		assert(result >= 0);
		return -1;
	}
	
	A = read_adjacency_matrix(argv[1]);
	if (A == NULL)
	{
		return_value = -1;
		goto main_cleanup;
	}

	g = (int*) calloc(A->n, sizeof(int));
	assert(g != NULL);

	elements_no = read_group_file(argv[2], g);

	if (elements_no == -1)
	{
		return_value = -1;
		goto main_cleanup;
	}
	
	B = create_modularity_mat(A);
	f = create_f(B, g, A->n, elements_no);
	gen_B_g = create_generalized_modularity_matrix(B, g, elements_no, f, A->n);
	print_matrix(gen_B_g, elements_no);
	
main_cleanup:
	if (g != NULL)
		free(g);
	if (B != NULL)
		free_matrix(B, A->n);
	if (f != NULL)
		free(f);
	if (gen_B_g != NULL)
		free_matrix(gen_B_g, elements_no);
	if (A != NULL)
		free_sparse_matrix_lst(A);

	#ifdef MTRACE
      /* End of memory allocation checks. */
    muntrace();
    #endif
	return return_value;
}

